import { Context } from "koa";
import { MenuService } from "../services/menu.service";
import { DateDTO } from "../validators/request.validator";
import { TokenPayload } from "../utils/auth.util";

export class MenuController {
  // 创建或更新菜单关联的菜品
  public static async upsertMenuDishes(ctx: Context) {
    try {
      const { date } = ctx.state.validatedParams as DateDTO;
      const dishesIdList = ctx.state.validatedBody.dishesIdList as number[];

      const menu = await MenuService.upsertMenu(date, dishesIdList);

      ctx.status = 200;
      ctx.body = {
        code: 200,
        message: "菜单更新成功",
        data: menu,
      };
    } catch (error) {
      console.error("菜单操作失败:", error);
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : "服务器内部错误",
      };
    }
  }

  // 根据日期获取菜单关联的菜品
  public static async getMenuByDate(ctx: Context) {
    try {
      const { userId } = ctx.state.validatedQuery as { userId?: number };
      const { date } = ctx.state.validatedParams as DateDTO;
      const menu = await MenuService.getMenuByDate(date, userId);

      ctx.body = {
        code: 200,
        data: menu,
      };
    } catch (error) {
      console.error("获取菜单失败:", error);
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : "服务器内部错误",
      };
    }
  }

  // 查询本周和下周菜单
  public static async getWeeksMenuDishes(ctx: Context) {
    try {
      const menus = await MenuService.getWeeksMenuDishes();

      ctx.body = {
        code: 200,
        data: menus,
      };
    } catch (error) {
      console.error("获取周菜单失败:", error);
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: error instanceof Error ? error.message : "服务器内部错误",
      };
    }
  }
}
